﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulldogCMS.Core.Storage.DataTypes;

namespace BulldogCMS.Core.Storage
{
    public class NodeVersion
    {
        private Dictionary<string, DataTypes.BaseDataType> _fieldValues = new Dictionary<string,DataTypes.BaseDataType>();

        internal Dictionary<string, DataTypes.BaseDataType> FieldValues
        {
            get
            {
                return this._fieldValues;
            }
        }

        public long ID
        {
            get;
            internal set;
        }

        public NodeVersionState State
        {
            get;
            internal set;
        }

        internal NodeVersionStorageState ProcessState
        {
            get;
            set;
        }

        public string LanguageName
        {
            get;
            internal set;
        }

        public int VersionNumber
        {
            get;
            internal set;
        }

        internal NodeVersion()
        {

        }

        public static NodeVersion CreateNodeVersion(int versionNumber, string languageName)
        {
            NodeVersion version = new NodeVersion()
            {
                VersionNumber = versionNumber,
                LanguageName = languageName,
                ProcessState = NodeVersionStorageState.Loaded
            };
            return version;
        }

        internal NodeVersion(string languageName)
        {
            this.LanguageName = languageName;
        }

        internal NodeVersion(IDictionary<string, DataTypes.BaseDataType> fieldValues)
        {
            foreach (var fieldPair in fieldValues)
            {
                this._fieldValues.Add(fieldPair.Key, fieldPair.Value);
            }
        }

        internal T GetValue<T>(string fieldName) where T : BaseDataType
        {


            if (this._fieldValues.ContainsKey(fieldName))
            {
                return this._fieldValues[fieldName] as T;
            }
            return default(T);
        }

        internal void SetValue<T>(string fieldName, T value) where T: BaseDataType
        {
            this._fieldValues[fieldName] = value;
        }

        internal void SetValue(string fieldName, BaseDataType value)
        {
            this._fieldValues[fieldName] = value;
        }

    }
}